package com.ahammertest.ycgk.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.ahammertest.ycgk.pay.PaySucEvent;
import com.shuoyue.ycgk.MyApplication;
import com.shuoyue.ycgk.ui.pay.PaySucActivity;
import com.shuoyue.ycgk.utils.XToast;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {

    private static final String TAG = "WXPayEntryActivity";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.content_main);

        MyApplication.getInstance().api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        MyApplication.getInstance().api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        Log.e(TAG, "onReq, errCode = " + req.getType());
    }

    @Override
    public void onResp(BaseResp resp) {
        Log.e(TAG, "onResp, res = " + resp.getType());
        Log.e(TAG, "onResp, errCode = " + resp.errCode + ":" + resp.errStr);

        if (resp.errCode == 0) {
            XToast.toast(this, "支付成功");
            EventBus.getDefault().post(new PaySucEvent(1));

            setResult(RESULT_OK);
            finish();
        } else {
            XToast.toast(this, "支付失败");
            EventBus.getDefault().post(new PaySucEvent(2));
            setResult(RESULT_CANCELED);
            finish();
        }
    }


}